package com.app.mymusic.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.app.mymusic.R;
import com.app.mymusic.utils.LogUtil;

import java.util.List;

/**
 * Created by Administrator on 2016-12-12.
 */

public abstract class SampleAdapter<T> extends RecyclerView.Adapter<MyViewHolder> {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private Context context;
    private List<T> mDatas;

    public SampleAdapter(List<T> mDatas, Context context) {
        this.mDatas = mDatas;
        this.context = context;
    }

    @Override
    public int getItemCount() {
        LogUtil.showLog("size:"+(mDatas.size()+1));
        return mDatas.size()==0?0:(mDatas.size()+1);

    }

    @Override
    public long getItemId(int position) {
        LogUtil.showLog("position;"+position);
        return position;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==TYPE_ITEM){
            return new MyViewHolder(getLayout(R.layout.item_charts,parent));
        }else if(viewType==TYPE_FOOTER){
            View view= LayoutInflater.from(context).inflate(R.layout.footview,parent,false);
            return new FootViewHolder(view);
        }else{
            return null;
        }
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        if(holder instanceof FootViewHolder) return ;
        convert(holder,mDatas.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1==getItemCount())
        {
            return TYPE_FOOTER;
        }else{
            return TYPE_ITEM;
        }
    }

    static class FootViewHolder extends MyViewHolder {

        public FootViewHolder(View view) {
            super(view);
        }
    }

    private View getLayout(int layoutId,ViewGroup parent) {
        return LayoutInflater.from(context).inflate(layoutId, parent,false);
    }

    public abstract void convert(MyViewHolder holder, T t);


}
